package com.vcaresolution.logistic.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.vcaresolution.logistic.form.BillofladingForm;
import com.vcaresolution.logistic.to.BillofladingTO;
import com.vcaresolution.logistic.util.ApplicationConstants;

/**
 * <code>BillofLading Controller</code> is used to track all login related actions
 *
 * @author Dharam C. Gusai
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
public class BillofLadingController extends BaseController{

	@ModelAttribute(value=ApplicationConstants.BILLOFLADINGFORM_MODELATTRIBUTE)
	public BillofladingForm createForm(){
		return new BillofladingForm();
	}
	
	private void loadRequiredDetailInModelObject(ModelAndView modelAndView){
		modelAndView.addObject(ApplicationConstants.JOBREGISTERLIST_MODELATTRIBUTE, getServiceRegistry().getJobRegisterService().findByAll());
		modelAndView.addObject(ApplicationConstants.MODELIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("MODE"));
		modelAndView.addObject(ApplicationConstants.PKGSTYPELIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("PKGSTYPE"));

		modelAndView.addObject(ApplicationConstants.VESSELLIST_MODELATTRIBUTE, getServiceRegistry().getVesselService().findByAll());
		modelAndView.addObject(ApplicationConstants.PORTLIST_MODELATTRIBUTE, getServiceRegistry().getPortService().findByAll());
		modelAndView.addObject(ApplicationConstants.CONTACTLIST_MODELATTRIBUTE, getServiceRegistry().getContactService().findByAll());
		modelAndView.addObject(ApplicationConstants.WEIGHTTYPELIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("WEIGHTTYPE"));
		modelAndView.addObject(ApplicationConstants.DOCTYPELIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("DOCTYPE"));
		modelAndView.addObject(ApplicationConstants.FREIGHTSTATUSLIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("FREIGHTSTATUS"));
		modelAndView.addObject(ApplicationConstants.NOOFBLLIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("NOOFBL"));

		//		modelAndView.addObject(ApplicationConstants.CURRENCYLIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("CURRENCY"));
//		modelAndView.addObject(ApplicationConstants.INCOTERMSLIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("INCOTERMS"));
//		modelAndView.addObject(ApplicationConstants.CONTAINERSIZELIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("CONTAINERTYPE"));
//		modelAndView.addObject(ApplicationConstants.DIRECTHUBLIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("DIRECTHUB"));
	}
	
	/**
	 * This method is called when the GET request has been made to the ADDBilloflading Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_BILLOFLADING_URL,method = RequestMethod.GET)
	public ModelAndView loadAddBilloflading() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(ApplicationConstants.ADD_BILLOFLADING_VIEW);
		modelAndView.addObject(ApplicationConstants.BILLOFLADINGFORM_MODELATTRIBUTE, new BillofladingForm());
		
		loadRequiredDetailInModelObject(modelAndView);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADD Billoflading Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_BILLOFLADING_URL,method = RequestMethod.POST)
	public ModelAndView processAddBilloflading(@Valid BillofladingForm billofladingForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			getServiceRegistry().getBillofladingService().add(billofladingForm);
			
			modelAndView = list();
		}else {
			
			loadRequiredDetailInModelObject(modelAndView);
		}
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the List Billoflading Request
	 */
	@RequestMapping(value=ApplicationConstants.LIST_BILLOFLADING_URL,method = RequestMethod.GET)
	public ModelAndView list() {
		final ModelAndView modelAndView = new ModelAndView();
		
		List<BillofladingTO> billofladingList = getServiceRegistry().getBillofladingService().findByAll();
		
		modelAndView.addObject(ApplicationConstants.BILLOFLADINGLIST_MODELATTRIBUTE, billofladingList);
		modelAndView.setViewName(ApplicationConstants.LIST_BILLOFLADING_VIEW);
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the ADD Billoflading Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_BILLOFLADING_URL,method = RequestMethod.GET)
	public  ModelAndView loadEditBilloflading(@RequestParam(value="blID") String blID) {
		ModelAndView modelAndView = new ModelAndView();
		
		Object billofladingObject = getServiceRegistry().getBillofladingService().findById(Integer.parseInt(blID));
		loadRequiredDetailInModelObject(modelAndView);
		
		modelAndView.addObject(ApplicationConstants.BILLOFLADINGFORM_MODELATTRIBUTE, billofladingObject);
		modelAndView.setViewName(ApplicationConstants.EDIT_BILLOFLADING_VIEW);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADD Billoflading Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_BILLOFLADING_URL,method = RequestMethod.POST)
	public ModelAndView processEditBilloflading(@Valid BillofladingForm billofladingForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			getServiceRegistry().getBillofladingService().update(billofladingForm);
			
			modelAndView = list();
		}
		else {
			loadRequiredDetailInModelObject(modelAndView);
		}
		
		return modelAndView;
	}
}